package feishu

import (
	"fmt"
	"net/http"
	"strings"
)

func SendText(apiUrl, msg string) bool {
	// json
	contentType := "application/json"
	// data
	sendData := `{
		"msg_type": "text",
		"content": {"text": "` + msg + `"}
	}`
	// request
	result, err := http.Post(apiUrl, contentType, strings.NewReader(sendData))
	if err != nil {
		fmt.Printf("post failed, err:%v\n", err)
		return false
	}
	defer result.Body.Close()
	return true
}

func SendTextAndUrl(apiUrl, title string, text string, link string, isAtAll bool) bool {
	contentType := "application/json"
	at := ""
	if isAtAll {
		at = `,{"tag":"at","user_id":"all"}`
		//text = text + " @所有人"
	}
	sendData := fmt.Sprintf(`
	{"msg_type":"post","content":{"post":{"zh_cn":{"title":"%v","content":[[{"tag":"a","text":"%v","href":"%v"}%v]]}}}}

	`, title, text, link, at)
	result, err := http.Post(apiUrl, contentType, strings.NewReader(sendData))
	if err != nil {
		fmt.Printf("post failed, err:%v\n", err)
		return false
	}
	defer result.Body.Close()
	return true
}
